package zombiegame;

import graphics.Field;
import graphics.Location;

/**
 * Parent Character class
 * @author pylaffon
 *
 */

public class Character {
    protected String name;
    // name of the character
    protected int healthPoints; // represents the health
    // (once down to 0, this character will be destroyed)
    // The Character's position.
    protected Location location;
    // The field occupied.
    protected Field field;
    // Whether the character is alive or not.
    protected boolean alive;
    protected TypeCharacter type;

    /**
     * Constructor of Character class.
     * @param name name of the character
     * @param healthPoints initial HP
     */
    public Character(String name, int healthPoints, Field field, Location location, TypeCharacter type) {
        this.name = name;
        this.healthPoints = healthPoints;
        this.field = field;
        setLocation(location);
        this.alive = true;
        this.type = type;
    }
    
    // Accessors
    public String getName() {
        return name;
    }
    public int getHealthPoints() {
        return healthPoints;
    }
    /**
     * Decrease the number of HP by a certain amount. HP cannot go below 0.
     * @param reduction number of HP to reduce
     */
    public void reduceHealthPoints(int reduction) {
        healthPoints = healthPoints - reduction;
        if (healthPoints < 0) {
            healthPoints = 0;
        }
    }
    /**
     * Output a character's saying to the screen
     * @param str what the character says
     */
    public void say(String str) {
        System.out.println(name + " says: " + str);
    }
    /**
     * Method triggered when the character described by the current object
     * meets another character, and does something to him (for example, attack).
     * @param c the other character that this character meets
     */
    public void encounterCharacter(Character c) {
        // Default action: do nothing
        System.out.println(name + " meets " + c.name + " and does not attack!");
    }
    
    /**
     * Method triggered on each character at the end of each turn.
     * 
     */
    public void endOfTurn() {
        // does something intelligent
    }
    
    /**
     * Return the character's location.
     * 
     * @return The character's location.
     */
    public Location getLocation() {
        return location;
    }
    
        
    /**
     * @return the type
     */
    public TypeCharacter getType() {
        return type;
    }

    /**
     * Place the character at the new location in the given field.
     * 
     * @param newLocation
     *            The character's new location.
     */
    protected void setLocation(Location newLocation) {
        if (location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }

    public Field getField() {
        return field;
    }
    
    /**
     * Indicate that the fox is no longer alive. It is removed from the field.
     */
    protected void setDead() {
        alive = false;
        if (location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }
    
    /**
     * Check whether the fox is alive or not.
     * 
     * @return True if the fox is still alive.
     */
    public boolean isAlive() {
        return alive;
    }
}
